package com.hnkywl.sts.service.kcgl;

import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cggl.CgdDao;
import com.hnkywl.sts.dao.kcgl.DhdDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cggl.Dhd;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-9
 * Time: 下午9:07
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DhdService {
    private DhdDao dhdDao;
    private CgdDao cgdDao;

    /**
     * 查询所有的到货单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllDhd(Long cgdId, Long cgdmxId, int pageNo, int pageSize) {
        Page<Dhd> dhdPage = this.dhdDao.findDhdList(cgdId, cgdmxId, pageNo, pageSize);
        List<Dhd> dhdList = dhdPage.getResult() == null ? new ArrayList<Dhd>() : dhdPage.getResult();
        return "{\"total\":" + dhdPage.getTotalCount() + ",\"rows\":" + new GsonBuilder().setDateFormat("yyyy-MM-dd").create().toJson(dhdList) + "}";
    }

    /**
     * 保存到货单
     *
     * @param dhd
     */
    @Transactional
    public void saveDhd(Dhd dhd) {
        Cgd cgd = cgdDao.get(dhd.getCgd().getId());
        if (cgd.getGys() != null)
            dhd.setGys((Qyjg) cgd.getGys());
        dhd.setDateCreated(new Date());
        dhd.setLastUpdated(dhd.getDateCreated());
        dhd.setCreateUser(YgService.getCurrentYg());
        dhd.setCreateOrgan(YgService.getCurrentYg().getZggs());
        dhd.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.dhdDao.save(dhd);
    }

    /**
     * 修改到货单
     *
     * @param dhd
     * @throws RuntimeException
     */
    @Transactional
    public void updateDhd(Dhd dhd) throws RuntimeException {
        Dhd dhdItem = this.dhdDao.get(dhd.getId());
        dhdItem = (Dhd) ReflectionUtils.getInstance(dhdItem, dhd);
        dhdItem.setLastUpdated(new Date());
        this.dhdDao.save(dhdItem);
    }

    /**
     * 删除到货单
     *
     * @param dhdId
     */
    @Transactional
    public void deleteDhd(Long dhdId) {
        Dhd dhd = dhdDao.get(dhdId);
        dhd.setLastUpdated(new Date());
        dhd.setDeleted(1);
        this.dhdDao.save(dhd);
    }

    @Transactional
    public String findDhdById(Long id) {
        Dhd dhd = this.dhdDao.get(id);
        List<Dhd> dhds = new ArrayList<Dhd>();
        dhds.add(dhd);
        String dhdJson = BaseUtils.fetchCollectionToJson(dhds, "id", "dhrq", "chh", "clshdh", "sender", "ysrq", "ydh", "thdd", "gys.id", "gys.qc", "wz.id", "wz.name", "wz.code", "wz.ggxh", "wz.dw", "cd", "yssl", "sssl", "sccj", "scrq", "cpzg", "qtzj", "bzzk", "swwg", "hdjybg", "sccc", "js", "cz", "jc", "wtcljg", "ysjl", "iswzpp", "iswzxh", "iswzname", "isgysname", "zt", "ysy", "bgy");
        return "[" + dhdJson + "]";
    }

    public Long findDhdIdByCgdmxId(Long id) {
        return dhdDao.findDhdByCgdmxId(id);
    }

    @Transactional
    public Dhd findDhdDetail(Long id) {
        return this.dhdDao.get(id);
    }

    @Autowired
    public void setDhdDao(DhdDao dhdDao) {
        this.dhdDao = dhdDao;
    }

    @Autowired
    public void setCgdDao(CgdDao cgdDao) {
        this.cgdDao = cgdDao;
    }
}
